#include <stdio.h>
#include <stdlib.h>

void print();
unsigned int Dayofyear(unsigned int year,unsigned int month,unsigned int day);
int JudgeYear();
void MonthandDay(unsigned int year,unsigned int yearday,unsigned int *pmonth,unsigned int *pday);

int main()
{
    int function;
    unsigned int year,month,day,yearday;
    unsigned int number;
    do
    {
        print();
        scanf("%d", &function);
        switch (function)
        {
        case 1:
            printf("Please enter year,month and day\n");
            scanf("%d %d %d", &year, &month, &day);
            number = Dayofyear(year, month, day);
            printf("Total number of days is %d\n", number);
            break;
        case 2:
            printf("Please enter year,totalday:\n");
            scanf("%d %d",&year,&yearday);
            MonthandDay(year,yearday,&month,&day);
            printf("The date is %d/%02d/%02d\n", year, month, day); //处理打印格式
            break;
        case 3:
            exit(0);
            break;
        default:
            printf("Wrong!\n");
        }
    } while (function != 3); //只要不输入3就不会终止程序而会一直打印菜单
    return 0;
}

void print()  //打印一个菜单
{
    printf("1.year/month/day->yearday\n");
    printf("2.year/yearday->year/month/day\n");
    printf("3.Exit\n");
    printf("Please enter the function you want to use:\n");
}

unsigned int Dayofyear(unsigned int year, unsigned int month, unsigned int day)
{
    int judge;
    int i;
    int monthday[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};   //写一个月份天数的列表
    judge=JudgeYear(year);
    for(i=1;i<month;i++)
    {
        day=day+monthday[judge][i];  //将月份的天数依次累加
    }
    return day;
}

int JudgeYear(unsigned int year)   //判断年份是闰年还是平年
{
    if((year%4==0&&year%100!=0)||(year%400==0))
    {
        return 1;
    }
    else 
    {
        return 0;
    }
}

void MonthandDay(unsigned int year, unsigned int yearday, unsigned int *month, unsigned int *day)
{
    unsigned int days_in_month[] = {31, 28, 31, 30, 31, 30,
                                    31, 31, 30, 31, 30, 31};
    // 判断是否为闰年
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    {
        days_in_month[1] = 29; // 闰年2月有29天
    }

    unsigned int temp = yearday;
    for (int i = 0; i < 12; i++)
    {
        if (temp <= days_in_month[i])
        {
            *month = i + 1;
            *day = temp;
            return;
        }
        temp -= days_in_month[i];
    }
}